/**
* This program is distributed "as-is" and is not
* documented beyond the scope of the source-code
* comments.											
*
* @author:	JR Lanteigne
* @class:	JET 1 Spring 2014
* @title:	schedule.cpp
*/
#define _CRT_SECURE_NO_WARNINGS 1

#ifdef _WIN32
#define _IS_WINDOWS 1
#define __kbhit() (_kbhit())
#include <conio.h> // getch, kbhit - dos or later only
#include <Windows.h> // SetConsoleTextAttributes
#else
#define _IS_WINDOWS 0
#define __kbhit() (0)
#endif

#define DEF -1
#define GREEN 0
#define BLUE 1
#define RED 2
#define CYAN 3
#define WHITE 4

#include <stdio.h> //printf, scanf
#include <stdlib.h> //system
#include <string.h> //strcpy
#include <time.h> // for time()

// global

int init = 0; // first run

void printday(unsigned int day); // convert 1 - 7 in day of the week
void cls(); // clear screen
void setColor(int color); // wrapper for Windows.h text attributes
void header(int fast);
void _wait(int msec); // delay in msec
int timer = 50; // default 50, unless timer is set by argument

int main(int argc, char **argv) {
	unsigned int _buffer = 0; // user input
	char go = 1; // condition for while
	if(argc > 1 && atoi(argv[1]) > 0) {
		timer = atoi(argv[1]);
	}

	if(!init) { header(1); }

	while(go) {
		go = 0; // we got in the loop so make sure we can get back out
		fflush(stdin); // start with fresh buffer
		printf  ("\n\nEnter a day of the week,\n" \
		"1 - Monday, 7 - Sunday (0 to quit): ");
		_buffer = -1;
		scanf("%u", &_buffer); // scan input buffer as unsigned int

		header(0);

		setColor(CYAN);

		switch(_buffer) {
			case 0:
				return 0; // this is the only exit point
			case 1:
			case 3:
			case 4:
			case 5:
				printday(_buffer);
				printf("*** PROG1018 ***\n");
				setColor(BLUE);
				printf("(pm) 1.30 - 3.20\n");
				break;
			case 2:
				printday(_buffer);
				printf("**** PROG1018 ****\n");
				setColor(BLUE);
				printf("(am) 10.30 - 11.20\n");
				break;
			case 6:
			case 7:
				printday(_buffer);
				printf("\n!! You have no school on that day !!\n");
				break;
			default:
				setColor(RED);
				printf("\nValue must be an integer >= 1 and <= 7\n");
				go = 1;
		}
		setColor(DEF);
	}

	//setColor(DEF);

	init++;
	main(1, 0);
}

void printday(unsigned int day) { // take user input and print its value
	char printable[10] = {NULL};
	switch(day) {
	case 1:
		strcpy(printable, "MONDAY");
		break;
	case 2:
		strcpy(printable, "TUESDAY");
		break;
	case 3:
		strcpy(printable, "WEDNESDAY");
		break;
	case 4:
		strcpy(printable, "THURSDAY");
		break;
	case 5:
		strcpy(printable, "FRIDAY");
		break;
	case 6:
		strcpy(printable, "SATURDAY");
		break;
	default:
		strcpy(printable, "SUNDAY"); // last possibility...
	}
	printf("\n%s\n_________________\n\n", printable);
}

void cls() {
	if(_IS_WINDOWS) {
		system("cls"); // win
	} else {
		system("clear"); // posix
	}
	setColor(DEF);
}

void setColor(int color) {
	if(_IS_WINDOWS) {
		switch(color) {
		case GREEN:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
			break;
		case CYAN:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			break;
		case RED:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
			break;
		case BLUE:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY | 
				BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
			break;
		case WHITE:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
			break;
		default:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		}
	} // do absolutely nothing
}
void header(int fast) {
	char _heading[] = "ANY KEY TO CONTINUE\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b========================\nCLASS SCHEDULE FOR JET 1\n\tJR LANTEIGNE    \n========================\n";

	cls();
	setColor(WHITE);
	for(int i = 0; i < strlen(_heading); i++) {
		printf("%c", _heading[i]);
		if(fast) { _wait(timer); }
	}
	setColor(DEF);
}

/* CLOCKS_PER_SEC is defined as the integer amount of cpu cycles
   per second; therefore, one must convert msec to seconds by
   dividing by 1000; then multiply the result by CLOCKS_PER_SEC.
*/
void _wait(int msec) {
	float _ticks = ((float)msec/1000)*CLOCKS_PER_SEC;
	clock_t  _toend = clock() + (int) _ticks;
	while(clock() < _toend && !__kbhit()); // waste cpu cycles, quits on kbhit
}